<?php

include_once (MODEL. "/Repository.php");
include_once (MODEL. "/OfferModel.php");
include_once (MODEL. "/WantModel.php");

class SearchRepository extends Repository
{
    public $collectionWant;
    public $collectionOffer;
    public $currentPage;
    public $pageUrl;
    public $inactivePageTag;
    public $totalRecords;
    public $pageUrllast='';
    
    public function __construct()
    {
        parent:: __construct();
        $this->collectionWant = $this->DB->selectCollection('Want');
        $this->collectionOffer = $this->DB->selectCollection('Offer');
    }
    
    public function GetListCategory() 
    {
        return $this->DB->selectCollection('Category')->find();
    }

    public function PageSet($currentPage, $pageUrl, $inactivePageTag, $filter,$switch)
    {
        $this->currentPage = $currentPage;
        $this->pageUrl = $pageUrl;
        $this->inactivePageTag = $inactivePageTag;
        if($switch == 1)
            $this->totalRecords = $this->collectionOffer->find($filter)->count();  
        else if($switch == 2)
            $this->totalRecords = $this->collectionWant->find($filter)->count();
    }               
    
    // Tính tổng số collection có ID category = $categoryID
    public function TotalByCategory($categoryID,$switch)
    {
    	$cond = array('CategoryID' => new MongoId($categoryID));
        if($switch == 1)
            return $this->collectionOffer->find($cond)->count();
        if($switch == 2)
            return $this->collectionWant->find($cond)->count();
    }
    
    // Search in the Offer
    public function searchOffer($content, $categoryID = 0, $cityId = 0, $start, $limit)
    {
        $cond = array();
        if($content != '')
            $cond = array('Name'=>array('$regex' => $content, '$options' => 'i'));
        if($categoryID != 0)
            $cond['CategoryID'] = new MongoId($categoryID);
        if($cityId != 0)
            $cond['CityId'] = new MongoId($cityId);
        
        $cursor = $this->collectionOffer->find($cond)->skip($start)->limit($limit);
        $listItem = array();
        while($cursor->hasNext()) 
        {
            $offer = new OfferModel();
            $result = $cursor->getNext();
            $offer->ID = $result['_id'];
            $offer->Name = $result['Name'];
            $offer->Location = $result['Location'];
            $offer->Image = $result['Image'];
            $offer->Description = $result['Description'];
            $offer->CreateDate = $result['CreateDate'];
            $offer->Street = $result['Street'];

            $user = $this->DB->selectCollection('Account')->findOne(array('_id' => new MongoId($result['AccountID'])));
            $offer->OwnerID = $user['_id'];
            $offer->OwnerName = $user['FirstName'];

            $listItem[] = $offer;
        }
        return $listItem;
    }
    //Search in the Want
    public function searchWant($content, $categoryID, $cityId, $start, $limit)
    {
        $cond = array();
        if($content != '')
            $cond = array('Name'=>array('$regex' => $content, '$options' => 'i'));
        if($categoryID != 0)
            $cond['CategoryID'] = new MongoId($categoryID);
        if($cityId != 0)
            $cond['CityId'] = new MongoId($cityId);
        
        $cursor = $this->collectionWant->find($cond)->skip($start)->limit($limit);
        $listItem = array();
        while($cursor->hasNext()) 
        {
            $want = new WantModel();
            $result = $cursor->getNext();
            $want->ID = $result['_id'];
            $want->Name = $result['Name'];
            $want->Location = $result['Location'];
            $want->Image = $result['Image'];
            $want->Description = $result['Description'];
            $want->CreateDate = $result['CreateDate'];
            $want->Street = $result['Street'];

            $user = $this->DB->selectCollection('Account')->findOne(array('_id' => new MongoId($result['AccountID'])));
            $want->OwnerID = $user['_id'];
            $want->OwnerName = $user['FirstName'];
            
            $listItem[] = $want;
        }
        return $listItem;
    }
    
}
?>
